<?php
/*
 * 前端网校控制器
 */
class SchoolonlineController extends BasicController {
    
    private function init(){
        Yaf_Registry::get('tokenPlugin')->checkAuth();
    }

    /*
    * 获取网校的列表
    */
    public function getlistAction(){
        $params = $this->getPost();
        
        $so_id = isset($params['id']) ? $params['id'] : '';
        $so_name = isset($params['name']) ? $params['name'] : '';
        $so_domain = isset($params['domain']) ? $params['domain'] : '';
        
        $return['code'] = '1';
        $return['msg'] = '获取失败！';
        $return['data'] = [];
        
        $schoolOnlineModel = Helper::load('School_online');
        
        $where = " isdelete = 0 and is_new = 1 ";
        $binds = [];
        
        if($so_id){
            $where.= " and so_id = ? ";
            $binds[] = $so_id;
        }
        
        if($so_name){
            $where.= " and so_name like '%".$so_name."%' ";
        }
        
        if($so_domain){
            $where.= " and so_domain = ? ";
            $binds[] = $so_domain;
        }
        
        $sql = "select `so_id`, `so_name`, `so_img`, `so_domain` "
                . " from `".TB_PREFIX."school_online`"
                . " where {$where}"
                . " order by so_id desc";

        $res = $schoolOnlineModel->CompileBinds($sql, $binds)->Select();
        
        $data = [];
        
        if(!empty($res) && is_array($res)){
            foreach($res as $item){
                $data[] = [
                    'id'    => $item['so_id'],
                    'name'  => $item['so_name'],
                    'img'   => $item['so_img'],
                    'domain'=> $item['so_domain']
                ];
            }
        }
        
        $return['code'] = '0';
        $return['msg'] = '操作成功！';
        $return['data'] = $data;
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 根据二级域名获取对应网校的so_id
    */
    public function getsoidAction(){
        $params = $this->getPost();
        
        $domain = isset($params['domain']) ? $params['domain'] : '';
        
        $return['code'] = '1';
        $return['msg'] = '获取失败或网校不存在！';
        $return['data'] = [];
        
        if(!$domain){//如果没有传递过来域名
            echo json_encode($return);
            return false;
        }
        
        $schoolOnlineModel = Helper::load('School_online');
        
        $info = $schoolOnlineModel->Where(['so_domain' => $domain, 'isdelete' => 0])->SelectOne();
        
        if(!empty($info) && is_array($info)){
            $return['code'] = '0';
            $return['msg'] = '操作成功！';
            $return['data'] = [
                'so_id' => $info['so_id']
            ];
        }
        
        echo json_encode($return);
        return false;
    }
    
    /*
    * 根据网校的so_id获取对应的网校二级域名 
    */
    public function getdomainAction(){
        $params = $this->getPost();
        
        $so_id = isset($params['id']) ? $params['id'] : '';
        
        $return['code'] = '1';
        $return['msg'] = '获取失败或网校不存在！';
        $return['data'] = [];
        
        if(!$so_id){//如果没有传递过来域名
            echo json_encode($return);
            return false;
        }
        
        $schoolOnlineModel = Helper::load('School_online');
        
        $info = $schoolOnlineModel->Where(['so_id' => $so_id, 'isdelete' => 0])->SelectOne();
        
        if(!empty($info) && is_array($info)){
            $return['code'] = '0';
            $return['msg'] = '操作成功！';
            $return['data'] = [
                'domain' => $info['so_domain']
            ];
        }
        
        echo json_encode($return);
        return false;
    }
    
}